//图片轮播
$(document).ready(function(){
    var interval=300;
    var count=1;
    $('.little-img').on('mouseenter','li',function(){
        clearInterval(start);
        var index=$('.little-img li').index(this);
        $(this).addClass('active').siblings().removeClass('active');
        $('.large-img').css('marginTop',-index*interval+'px');
        count=index;
    }).mouseleave(function(){
        start=setInterval(lunbo,2000);
    });
    function lunbo(){
        if(count==3){
            count=0;
        }
        $('.large-img').css('marginTop',-count*interval+'px');
        var i=count+1;
        $('.little-img li:nth-child('+i+')').addClass('active').siblings().removeClass('active');
        count++;
    };
    var start=setInterval(lunbo,2000);
    $('.cate_list').on('mouseover','li',function(){
    	$(this).addClass('active').siblings('li').removeClass('active');
    	$('.type_block').css('display','block');
    	var id = $(this).attr('data-id');
    	$('.type_block').children("[data-id='"+id+"']").css('display','block').siblings('div').css('display','none'); 
    })
    $('.type_block').on('mouseover',function(){
    	$(this).css('display','block');
    	$('.type_block').on('mouseleave',function(){
        	$(this).css('display','none');
        	$('.cate_list').find('li').removeClass('active');
        })
    });
    $('.cate_block').on('mouseleave',function(){
    	$('.type_block').css('display','none');
    	$('.cate_list').find('li').removeClass('active');
    })
    $('body').on('click',':not(.search-box)',function(){
    	$('.search_product_list').css('display','none');
    })
    //点击分类进入具体分类页面
    $('.cate_list').on('click','a',function(e){
    	e.preventDefault();
    	var id = $(this).parent('li').attr('data-id');
    	searchProduct(id);
    })
    $('.type_block').on('click','dd a',function(e){
    	e.preventDefault();
    	var id = $(this).attr('data-id');
    	searchProduct(id);
    }).on('click','dt a',function(e){
    	e.preventDefault();
    	var id = $(this).parent().attr('data-id');
    	searchProduct(id);
    });
    $('.search_product_list').on('click','li',function(){
    	var id = $(this).attr('data-id');
    	searchProduct(id);
    })
//    加载分类
    $.ajax({
		type:"post",
		url:"/diconet/productCategory/findAllProdCategory",
		success:function(data){
			var typeList = data.data;
		    var cateList = "";
		    var cateBlock = "";
		    for(var i = 0;i<typeList.length;i++){
		    	if(typeList[i].parentId == ""){
		    		cateList += "<li data-id='"+typeList[i].id+"'><a href=''>"+typeList[i].categoryName+"</a></li>";
		    		cateBlock += "<div class='cate_item' data-id='"+typeList[i].id+"'></div>"
		    	}
		    	$('.cate_list').html(cateList);
		    	$('.type_block').html(cateBlock);
		    	
		    };
		    $('.cate_item').each(function(){
		    	var cateItem = "";
		    	var id = $(this).attr('data-id');
		    	for(var i = 0;i<typeList.length;i++){
		        	if(typeList[i].parentId == id){
		        		cateItem += "<dl class='type_item'>";
		        		cateItem += "<dt data-id='"+typeList[i].id+"'>";
		        		cateItem += "<a href=''>"+typeList[i].categoryName+"</a>";
		        		cateItem += "<i>></i>";
		        		cateItem += "</dt>";
		        		cateItem += "<dd class='type_detail'>";
		        		for(var j = 0;j<typeList.length;j++){
		        			if(typeList[j].parentId == typeList[i].id){
		        				cateItem += "<a href='' data-id='"+typeList[j].id+"'>"+typeList[j].categoryName+"</a>"
		        			}
		        		}
		        		cateItem += "</dd>";
		        		cateItem += "</dl>";
		        	}
		        };
		        $(this).html(cateItem);
		    })
		}
	})
//	加载商品和广告图片
	$.ajax({
		type:"post",
		url:path + "/product/product/indexProductList",
		success:function(data){
			var list = data.data;
			var bigImgList = "";
			var imgList = "";
			var productList = "";
			for(var i = 0;i < list.length;i++){
				if(list[i].activityType == "indexAd"){
					bigImgList += "<img src='/docs/"+list[i].productImg+"' alt=''/>";
					imgList += "<li><img src='/docs/"+list[i].productImg+"' alt=''/></li>";
				}else if(list[i].activityType == "indexProductList"){
					productList += "<div class='product-item special fl'>";
					productList += "<div>";
					productList += "<div class='img-block'>";
					productList += "<img src='/docs/"+list[i].productImg+"' alt=''/>";
					productList += "</div>";
					productList += "<div class='product-txt'>";
					productList += "<p>"+list[i].productName+"</p>";
					productList += "<p class='price'><b>￥"+list[i].minPrice+"-￥"+list[i].maxPrice+"</b></p>";
					productList += "</div>";
					productList += "<button class='to_detail'>查看详情</button>";
					productList += "</div>";
					productList += "</div>"
				}
			};
			$('.large-img').html(bigImgList);
			$('.little-img').html(imgList);
			$('.product-list').html(productList);
		}
	})
});
function searchProduct(id){
	window.location.href = path + "/product/product/goProductListPage?id="+id;
}
//搜索框模糊查询
function search(tag) {
	var keyWord = $(tag).val();
	if(keyWord == ""){
		$(tag).siblings('.search_product_list').css('display','none');
	}else{
		$(tag).siblings('.search_product_list').css('display','block');
	}
//	$.ajax({
//		type:"post",
//		url:,
//		success:function(data){
//			console.log(data);
//			var html = "";
//			for(var i = 0;i<data.data.length;i++){
//				html += "<li data-id='"+data.data[i].id+"'>"+data.data[i].productName+"</li>"
//			}
//			$('.search_product_list').html(html);
//		},
//		error:function(){}
//	})
}

